package cn.iot.sys.service.impl;

import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysBedDao;
import cn.iot.sys.dao.SysStudentDao;
import cn.iot.sys.entity.SysBed;
import cn.iot.sys.service.SysBedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SysBedServiceImpl implements SysBedService {
    @Resource
    private SysBedDao sysBedDao;
    @Autowired
    private SysStudentDao sysStudentDao;
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg getBedByHostelId(int hostelId){
        logger.info("SysBedServiceImpl getBedByHostelId hostelId{}",hostelId);
        List<SysBed> bedByHostelId = sysBedDao.getBedByHostelId(hostelId);
        ResultMsg msg = new ResultMsg(200,"成功",bedByHostelId);
        return msg;
    }

    @Override
    public ResultMsg updIsOccupyById(Map<String,Object> map) {
        logger.info("SysBedServiceImpl updIsOccupyById map{}",map);
        int count = sysBedDao.updIsOccupyById(map);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",null);
            return msg;
        }
        msg = new ResultMsg(500,"成功",null);
        return msg;
    }

    @Override
    public ResultMsg delIsOccupyBystuId(String studentId) {
        logger.info("SysBedServiceImpl delIsOccupyBystuId studentId{}",studentId);
        int count = sysBedDao.delIsOccupyBystuId(studentId);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",null);
            return msg;
        }
        msg = new ResultMsg(500,"成功",null);
        return msg;
    }

    @Transactional
    @Override
    public ResultMsg addHostelByStuId(Map<String, Object> map) {
        logger.info("SysBedServiceImpl addHostelByStuId map{}",map);
        String studentId = (String) map.get("studentId");
        sysStudentDao.updHostelIdByStudentId(map);
        sysBedDao.delIsOccupyBystuId(studentId);
        int count = sysBedDao.addHostelByStuId(map);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",null);
            return msg;
        }
        msg = new ResultMsg(500,"成功",null);
        return msg;
    }

    @Override
    public ResultMsg delStuBedById(Map<String, Object> map) {
        logger.info("SysBedServiceImpl addHostelByStuId map{}",map);
        String studentId = (String) map.get("studentId");
        int hostelId = 32;
        map.put("hostelId",hostelId);
        sysStudentDao.updHostelIdByStudentId(map);
        int count = sysBedDao.delIsOccupyBystuId(studentId);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",null);
            return msg;
        }
        msg = new ResultMsg(500,"成功",null);
        return msg;
    }
}
